from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

'''
utility 工具
'''

class BoxDriver:

    def __init__(self,browser='Chrome'):
        if browser == 'Chrome':
            self.driver = webdriver.Chrome()
        elif browser == 'Firefox':
            self.driver = webdriver.Firefox()
        elif browser == 'Ie':
            self.driver = webdriver.Ie()
        elif browser == 'Edge':
            self.driver = webdriver.Edge()
        elif browser == 'Safari':
            self.driver = webdriver.Safari()
        elif browser == 'Opera':
            self.driver = webdriver.Opera()
        
    def get(self,url):
        '''
        打开网页
        url 网页的地址
        '''
        self.driver.get(url)

    def maximize_window(self):
        '''
        窗口最大化
        '''
        self.driver.maximize_window()

    def implicitly_wait(self,seconds=10):
        '''
        隐式等待
        seconds 最大等待时间，单位是秒
        '''
        self.driver.implicitly_wait(seconds)

    def convert_selector_to_locator(self,selector):
        '''
        将自定义的定位方式转换为Selenium的标准定位方式
        'id,account'  -> (By.ID, 'account')
        selector    自定义的定位方式
        '''
        # 拆分字符串
        by,value = selector.split(',')
        # 去除多余的空格
        by,value = by.strip(),value.strip()
        if by == 'id' or by == 'i':
            locator = (By.ID,value)
        elif by == 'name' or by == 'n':
            locator = (By.NAME,value)
        elif by == 'class_name' or by == 'c':
            locator = (By.CLASS_NAME,value)
        elif by == 'tag_name' or by == 't':
            locator = (By.TAG_NAME,value)
        elif by == 'link_text' or by == 'l':
            locator = (By.LINK_TEXT,value)
        elif by == 'partial_link_text' or by == 'p':
            locator = (By.PARTIAL_LINK_TEXT,value)
        elif by == 'xpath' or by == 'x':
            locator = (By.XPATH,value)
        elif by == 'css_selector' or by == 'css':
            locator = (By.CSS_SELECTOR,value)
        return locator

    def locate_element(self,selector):
        '''
        定位单个元素
        selector    自定义的定位方式
        '''
        # 将自定义定位方式转换为标准定位饭方式
        locator = self.convert_selector_to_locator(selector)
        # 返回定位到的元素
        return self.driver.find_element(*locator)

    def locate_elements(self,selector):
        '''
        定位多个元素
        selector    自定义的定位方式
        '''
        # 将自定义定位方式转换为标准定位饭方式
        locator = self.convert_selector_to_locator(selector)
        # 返回定位到的元素
        return self.driver.find_elements(*locator)

    def input(self,selector,text):
        '''
        向元素中写入文本
        selector    自定义的定位方式
        text        文本
        '''
        # 定位元素
        element = self.locate_element(selector)
        # 写入文本
        element.send_keys(text)

    def click(self,selector):
        '''
        向元素中写入文本
        selector    自定义的定位方式
        '''
        # 定位元素
        element = self.locate_element(selector)
        # 单击
        element.click()

    def switch_to_frame(self,selector):
        '''
        进入frame
        selector    自定义的定位方式
        '''
        # 定位iframe元素
        iframe = self.locate_element(selector)
        # 切换到iframe中
        self.driver.switch_to.frame(iframe)

    def select_by_index(self,selector,index):
        '''
        根据下标进行选择
        selector    自定义的定位方式
        index       下标
        '''
        select = self.locate_element(selector)
        depts = Select(select)
        depts.select_by_index(index) # 根据下标进行选择，下标从0开始

    def select_by_value(self,selector,value):
        '''
        根据value属性的值进行选择
        selector    自定义的定位方式
        value       value属性的值
        '''
        select = self.locate_element(selector)
        depts = Select(select)
        depts.select_by_value(value) # 根据下标进行选择，下标从0开始

    def select_by_visible_text(self,selector,visible_text):
        '''
        根据可见文本进行选择
        selector    自定义的定位方式
        visible_text   可见文本
        '''
        pass
        select = self.locate_element(selector)
        depts = Select(select)
        depts.select_by_visible_text(visible_text) # 根据下标进行选择，下标从0开始


    def close(self):
        '''
        关闭当前窗口
        '''
        self.driver.close()

    def quit(self):
        '''
        退出浏览器
        '''
        self.driver.quit()
  

if __name__ == "__main__":
    BoxDriver().convert_selector_to_locator('id,account')